package com.zhenyi.xiaohuoche.admin.web;

import java.math.BigDecimal;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xhc.common.model.XhcPresent;

public class PresentController extends XHCAdminController {
	@Override
	public void list() {
		SqlPara sqlPara = xhcDb.getSqlPara("XHCFindPresents");
		Page<XhcPresent> pages = XhcPresent.dao.paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(pages);
		setJsonItems(pages.getList());
		render("/xhc/admin/present/list.html");
	}

	@Override
	public void delete() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure("id is null");
			return;
		}
		XhcPresent info = XhcPresent.dao.findById(id);
		if (info == null) {
			renderFailure("info is null");
			return;
		}
		info.setFlag(1);
		Boolean flag = info.update();
		if (!flag) {
			renderMsg("删除失败");
			return;
		}
		renderSuccess();
	}

	@Override
	public void info() {
		Long id = getParaToLong("id");
		XhcPresent info = XhcPresent.dao.findById(id);
		if (info == null) {
			info = new XhcPresent();
		}
		setInfoItem(info);
		// renderResult(info);
		render("/xhc/admin/present/info.html");
	}

	@Override
	public void submit() {
		Long id = getParaToLong("id");
		String title = getPara("title");
		String desc = getPara("desc");
		String cover = getPara("cover");
		String images = getPara("images");
		Integer count = getParaToInt("count");
		BigDecimal price = new BigDecimal(getPara("price", "0"));
		XhcPresent info = XhcPresent.dao.findById(id);
		if (info == null) {
			info = new XhcPresent();
			info.save();
		}
		info.setTitle(title);
		info.setDesc(desc);
		info.setCover(cover);
		info.setImages(images);
		info.setCount(count);
		info.setPrice(price);
		Boolean flag = info.update();
		if (!flag) {
			renderFailure("保存失败");
			return;
		}
		renderSuccess();
	}
}
